package com.zhaojonhy.zhaopangwb.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
//import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.bean.PicUrls;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016/9/13.
 */
public class StatusGridImgsAdapter extends BaseAdapter {

    final static String TAG = StatusAdapter.class.getSimpleName() ;

    private Context context ;
    private ArrayList<PicUrls> picUrlsBeen ;
    private ImageLoader imageLoader ;

    public StatusGridImgsAdapter(Context context ,ArrayList<PicUrls> picUrlsBeen ) {
        this.context = context ;
        this.picUrlsBeen = picUrlsBeen ;
        imageLoader = ImageLoader.getInstance() ;
    }

    @Override
    public int getCount() {
        return picUrlsBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return picUrlsBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null ;
        if( convertView == null ){
            holder = new Holder() ;
            convertView = View.inflate(context, R.layout.item_grid_image,null) ;
            holder.mImageView = (ImageView)convertView.findViewById(R.id.iv_image) ;
            holder.tv_pic_flash = (TextView)convertView.findViewById(R.id.tv_pic_flash) ;
            convertView.setTag(holder);
        }else {
            holder = (Holder)convertView.getTag();
        }

        holder.tv_pic_flash.setVisibility(View.GONE);
        GridView gridView = (GridView) parent ;

        //计算宽高
        //item间的水平间距
        int horizontalsPacing = gridView.getHorizontalSpacing() ;
        int numColumns = gridView.getNumColumns() ;
        int itemWidth = ( gridView.getWidth() - (numColumns - 1)*horizontalsPacing
                - gridView.getPaddingLeft() - gridView.getPaddingRight() ) / numColumns ;

        LayoutParams params = new LayoutParams(itemWidth,itemWidth) ;
        //把宽高设置上imageView
        holder.mImageView.setLayoutParams(params);

        PicUrls picUrls = (PicUrls)getItem(position) ;

        imageLoader.displayImage(picUrls.getBmiddle_pic(), holder.mImageView);
        int lastdex = picUrls.getBmiddle_pic().lastIndexOf(".") +1 ;
        if(picUrls.getBmiddle_pic().substring(lastdex).equals("gif")){
            holder.tv_pic_flash.setVisibility(View.VISIBLE);
        }else {
            holder.tv_pic_flash.setVisibility(View.GONE);
        }
        Log.d(TAG,"URL-->"+"Statuse-->"+"picUrls.gif:"+picUrls.getThumbnail_pic().substring(lastdex)) ;
        return convertView;
    }

    public static class Holder{
        public ImageView mImageView ;
        public TextView tv_pic_flash ;

    }
}
